//
//  SwitchMiddleView.m
//  RandomAccount
//
//  Created by apple on 2020/9/15.
//  Copyright © 2020 cuibo. All rights reserved.
//

#import "SwitchMiddleView.h"
@implementation SwitchMiddleView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self addSubviews];
    }
    return self;
}
- (void)addSubviews {
    CGFloat swith_W = 70;
    CGFloat swith_X = self.frame.size.width / 2;
    UISegmentedControl * s = [[UISegmentedControl alloc] initWithItems:@[@"要",@"不要"]];
    s.frame = CGRectMake(swith_X, 0, swith_W, 30);
    [s setSelectedSegmentTintColor:[UIColor whiteColor]];
    s.layer.borderWidth = 1;
    [s setSelectedSegmentTintColor:[UIColor whiteColor]];
    [s setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [s setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor placeholderTextColor]} forState:UIControlStateSelected];
    s.layer.borderColor = [UIColor whiteColor].CGColor;
    [s addTarget:self action:@selector(segmetnClick:) forControlEvents:UIControlEventValueChanged];
    s.selectedSegmentIndex = 0;
    [self segmetnClick:s];
    [self addSubview:s];
    
    UILabel * tipsLb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, swith_X, 30)];
    tipsLb.text = @"允许重复数字：";
    tipsLb.textAlignment = NSTextAlignmentRight;
    tipsLb.textColor = [UIColor whiteColor];
    [self addSubview:tipsLb];
    
    UIButton * restartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    restartBtn.frame = CGRectMake(swith_X + swith_W + 5, 0, 30, 30);
    restartBtn.backgroundColor = [UIColor whiteColor];
    [restartBtn addTarget:self action:@selector(restartClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:restartBtn];
}
- (void)segmetnClick:(UISegmentedControl *)sender {
    if (self.block) {
        self.block(!sender.selectedSegmentIndex);
    }
}
- (void)restartClick {
    if (self.hisblock) {
        self.hisblock();
    }
}
@end
